package endpoint

import (
	"context"
	"github.com/go-kit/kit/endpoint"
	"logistics-golang/service"
)

type UserEndpoints struct {
	RegisterEndpoint endpoint.Endpoint
	LoginEndpoint    endpoint.Endpoint
}

// LoginRequest 登陆请求结构体
type LoginRequest struct {
	Email    string
	Password string
}

// LoginResponse 登陆用户信息响应结构体
type LoginResponse struct {
	UserInfo *service.UserInfoDTO `json:"user_info"`
}

// MakeLoginEndpoint 登陆 Endpoint 实现
func MakeLoginEndpoint(userService service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		// 请求
		req := request.(*LoginRequest)
		// service 处理
		userInfo, err := userService.Login(ctx, req.Email, req.Password)
		// 响应
		return &LoginResponse{UserInfo: userInfo}, err

	}
}

// RegisterRequest 注册请求结构体
type RegisterRequest struct {
	Username string
	Email    string
	Password string
}

type RegisterResponse struct {
	// json 返回字段
	UserInfo *service.UserInfoDTO `json:"user_info"`
}

// MakeRegisterEndpoint 注册 Endpoint 实现
func MakeRegisterEndpoint(userService service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		req := request.(*RegisterRequest)
		userInfo, err := userService.Register(ctx, &service.RegisterUserVO{
			Username: req.Username,
			Password: req.Password,
			Email:    req.Email,
		})
		return &RegisterResponse{UserInfo: userInfo}, err

	}
}
